package com.test.producer.controller;

import com.alibaba.fastjson.JSON;
import com.test.producer.config.RabbitConfig;
import com.test.producer.hello.HelloSender;
import com.test.producer.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
@RestController
public class SendController {
    @Autowired
    HelloSender helloSender;

    @GetMapping("/s")
    public Object test(Integer num) {
        for (int i = 0; i < num; i++) {
            int finalI = i;
            new Thread(() -> {
                User user = new User();
                user.setName("" + finalI);
                user.setPass(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + finalI);
                helloSender.sendMessage("", RabbitConfig.USER_QUEUE, JSON.toJSONString(user));
            }).start();
        }
        return "ok";
    }
    @GetMapping("/ss")
    public Object test(String name) throws InterruptedException {
        log.info("user"+name.hashCode());
        synchronized (name){
            log.info("获取锁成功"+name);
            Thread.sleep(5000);
        }
        return "ok";
    }
}
